home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / misc / thesource7.lha / Utilities / xemhex.lha / xemhex / source / HEXfuncs.c < prev    next >
C/C++ Source or Header  |  1993-05-23  |  11KB  |  554 lines

  1. /* $Revision Header built by Ueli Kaufmann ********** (please edit) **********
  2. **
  3. ** © Copyright by Ueli `Lonely Rider' Kaufmann
  4. **
  5. ** File             : xemhex.library
  6. ** Created on       : Sonntag 23-Mai-93  15:17:12
  7. ** Created by       : U. Kaufmann (Ueli_Kaufmann@augs1.adsp.sub.org)
  8. ** Current revision : V4.5
  9. **
  10. **
  11. ** Purpose
  12. ** -------
  13. **     HEX Emulator ala XEm
  14. **
  15. **
  16. ** Revision V4.5
  17. ** -------------
  18. **   - first release
  19. **
  20. *********************************************************************************/
  21.  
  22. #include "ownincs/HEXConsole.h"
  23.  
  24.  
  25.  
  26. /* allowed globals.. */
  27. struct    ExecBase            *SysBase;
  28. struct    DosLibrary        *DOSBase;
  29. struct    GfxBase            *GfxBase;
  30. struct    IntuitionBase    *IntuitionBase;
  31. struct    Library            *DiskfontBase;
  32. struct    Library            *KeymapBase;
  33. struct    Library            *UtilityBase;
  34.  
  35.  
  36. /* utils.a */
  37. VOID *GetSucc(VOID *);
  38. VOID *GetHead(VOID *);
  39. VOID *GetTail(VOID *);
  40. VOID *GetPred(VOID *);
  41. VOID BZero(VOID *, LONG);
  42. VOID BSet(VOID *, LONG, LONG);
  43.  
  44. STATIC VOID HEX_internalsettings(struct HEXConsole *con);
  45. STATIC BOOL HEX_parseoptions(struct HEXConsole *con, STRPTR optionsBuffer);
  46.  
  47.  
  48. VOID __saveds __asm __UserLibCleanup(register __a6 struct Library *libBase)
  49. {
  50.     if(DiskfontBase != NULL)
  51.     {
  52.         CloseLibrary(DiskfontBase);
  53.         DiskfontBase = NULL;
  54.     }
  55.  
  56.     if(KeymapBase != NULL)
  57.     {
  58.         CloseLibrary(KeymapBase);
  59.         KeymapBase = NULL;
  60.     }
  61.  
  62.     if(UtilityBase != NULL)
  63.     {
  64.         CloseLibrary(UtilityBase);
  65.         UtilityBase = NULL;
  66.     }
  67.  
  68.     if(IntuitionBase != NULL)
  69.     {
  70.         CloseLibrary((struct Library *)IntuitionBase);
  71.         IntuitionBase = NULL;
  72.     }
  73.  
  74.     if(GfxBase != NULL)
  75.     {
  76.         CloseLibrary((struct Library *)GfxBase);
  77.         GfxBase = NULL;
  78.     }
  79.  
  80.     if(DOSBase != NULL)
  81.     {
  82.         CloseLibrary((struct Library *)DOSBase);
  83.         DOSBase = NULL;
  84.     }
  85. }
  86.  
  87.  
  88. int __saveds __asm __UserLibInit(register __a6 struct Library *libBase)
  89. {
  90.     SysBase = *(struct ExecBase **)4;
  91.  
  92.     if((DOSBase            = (struct DosLibrary *)OpenLibrary("dos.library", 37)) == NULL)
  93.         return(1);
  94.  
  95.     if((IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37)) == NULL)
  96.         return(1);
  97.  
  98.     if((GfxBase            = (struct GfxBase *)OpenLibrary("graphics.library", 37)) == NULL)
  99.         return(1);
  100.  
  101.     if((UtilityBase    = OpenLibrary("utility.library", 37)) == NULL)
  102.         return(1);
  103.  
  104.     if((KeymapBase        = OpenLibrary("keymap.library", 37)) == NULL)
  105.         return(1);
  106.  
  107.     if((DiskfontBase    = OpenLibrary("diskfont.library", 33)) == NULL)
  108.         return(1);
  109.  
  110.  
  111.     return(0);
  112. }
  113.  
  114.  
  115. VOID ProcessData(struct HEXConsole *con, UBYTE *buffer, ULONG length)
  116. {
  117.     con->wcon->io_Length    = length;
  118.     con->wcon->io_Data    = (APTR)buffer;
  119.     con->wcon->io_Command= CMD_WRITE;
  120.     DoIO((struct IORequest *)con->wcon);
  121. }
  122.  
  123.  
  124. BOOL __saveds __asm XEmulatorSetup(register __a0 struct XEM_IO *io)
  125. {
  126.     struct HEXConsole *con;
  127.     
  128.     io->xem_console = NULL;
  129.  
  130.     if(con = AllocVec(sizeof(struct HEXConsole), MEMF_ANY | MEMF_CLEAR))
  131.     {
  132.         con->io = io;            /* for easier passing..(-; */
  133.         io->xem_console = con;
  134.  
  135.         if(con->wconport = CreatePort(NULL, 0))
  136.         {
  137.             if(con->wcon = CreateStdIO(con->wconport))
  138.             {
  139.                 STATIC struct TextAttr topazAttr8 = { "topaz.font", 8, FS_NORMAL, FPF_ROMFONT };
  140.  
  141.                 if(con->topazFont8 = OpenFont(&topazAttr8))
  142.                 {
  143.                     STATIC struct TextAttr topazAttr11 = { "topaz.font", 11, FS_NORMAL, FPF_DISKFONT };
  144.  
  145.                     if((con->topazFont11 = OpenFont(&topazAttr11)) == NULL)
  146.                         con->topazFont11 = OpenDiskFont(&topazAttr11);
  147.  
  148.                     return(TRUE);
  149.                 }
  150.             }
  151.         }
  152.     }
  153.  
  154.     return(FALSE);
  155. }
  156.  
  157.  
  158. VOID __saveds __asm XEmulatorCleanup(register __a0 struct XEM_IO *io)
  159. {
  160.     struct HEXConsole *con = (struct HEXConsole *)io->xem_console;
  161.  
  162.     if(con != NULL)
  163.     {
  164.         *con->io->xem_signal = 0;
  165.  
  166.         if(con->wcon->io_Device != NULL)
  167.             CloseDevice((struct IORequest *)con->wcon);
  168.  
  169.         if(con->wcon != NULL)
  170.             DeleteStdIO(con->wcon);
  171.  
  172.         if(con->wconport != NULL)
  173.             DeletePort(con->wconport);
  174.  
  175.         if(con->topazFont8 != NULL)
  176.             CloseFont(con->topazFont8);
  177.  
  178.         if(con->topazFont11 != NULL)
  179.             CloseFont(con->topazFont11);
  180.  
  181.         FreeVec(con);
  182.     }
  183. }
  184.  
  185.  
  186. BOOL __saveds __asm XEmulatorOpenConsole(register __a0 struct XEM_IO *io)
  187. {
  188.     struct HEXConsole *con = (struct HEXConsole *)io->xem_console;
  189.  
  190.     if(con != NULL)
  191.     {
  192.         struct RastPort *rp=con->io->xem_window->RPort;
  193.  
  194.         if(con->io->xem_font->tf_XSize != 8)
  195.         {
  196.             if(con->io->xem_font->tf_YSize > 8  &&  con->topazFont11 != NULL) 
  197.                 SetFont(rp, con->topazFont11);
  198.             else
  199.                 SetFont(rp, con->topazFont8);
  200.         }
  201.         else
  202.             SetFont(rp, con->io->xem_font);
  203.  
  204.         con->wcon->io_Data    = (APTR)con->io->xem_window;
  205.         con->wcon->io_Length    = sizeof(struct Window);
  206.         if(!OpenDevice("console.device", 0, (struct IORequest *)con->wcon, 0))
  207.             return(TRUE);    
  208.         else
  209.             con->wcon->io_Device = NULL;
  210.     }
  211.  
  212.     return(FALSE);    
  213. }
  214.  
  215.  
  216. VOID __saveds __asm XEmulatorCloseConsole(register __a0 struct XEM_IO *io)
  217. {
  218.     struct HEXConsole *con = (struct HEXConsole *)io->xem_console;
  219.  
  220.     if(con)
  221.     {
  222.         if(con->wcon->io_Device)
  223.         {
  224.             CloseDevice((struct IORequest *)con->wcon);
  225.             con->wcon->io_Device = NULL;
  226.         }
  227.     }
  228. }
  229.  
  230.  
  231. #include <devices/conunit.h>
  232.  
  233. VOID __saveds __asm XEmulatorWrite(register __a0 struct XEM_IO *io, register __a1 UBYTE *buf, register __d0 ULONG buflen)
  234. {
  235.     struct HEXConsole *con = (struct HEXConsole *)io->xem_console;
  236.     struct ConUnit    *unit = (struct ConUnit *)con->wcon->io_Unit;
  237.  
  238.     UBYTE    dummyBuffer[40], c;
  239.     LONG    fit, spaces, current;
  240.  
  241.     buflen = (((LONG)buflen) == -1) ? strlen(buf) : buflen;
  242.  
  243.     fit        = (unit->cu_XMax + 1) / 4;
  244.     current    = unit->cu_XCCP / 3;
  245.  
  246.     if(current >= fit  ||  (unit->cu_XCCP % 3))
  247.     {
  248.         ProcessData(con, "\r\n", 2);
  249.         current    = 0;
  250.     }
  251.  
  252.     while(buflen-- > 0)
  253.     {
  254.         c = *buf++;
  255.  
  256.         spaces = (fit - current) * 3 + current - 3;
  257.  
  258.         if((c > ' '  &&  c < 127)  ||  c > 160)
  259.             sprintf(dummyBuffer,"%02lx \33[%ldC%lc\33[%ldD", c, spaces, c, spaces + 1);
  260.         else
  261.             sprintf(dummyBuffer,"%02lx \33[%ldC.\33[%ldD", c, spaces, spaces + 1);
  262.  
  263.         ProcessData(con, dummyBuffer, strlen(dummyBuffer));
  264.  
  265.         if(current++ == (fit-1))
  266.         {
  267.             current = 0;
  268.             ProcessData(con, "\r\n", 2);
  269.         }
  270.     }
  271. }
  272.  
  273.  
  274. BOOL __saveds __asm XEmulatorSignal(register __a0 struct XEM_IO *io, register __d0 ULONG sig)
  275. {
  276.     return(TRUE);
  277. }
  278.  
  279.  
  280. STATIC BOOL HandleMacroKeys(struct HEXConsole *con, struct IntuiMessage *imsg, UBYTE chr)
  281. {
  282.     if(con->macrokeys  &&  con->io->xem_process_macrokeys)
  283.     {
  284.         struct XEmulatorMacroKey *key;
  285.         BOOL shift, alt, ctrl;
  286.         UWORD qual;
  287.  
  288.         shift    = imsg->Qualifier & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT);
  289.         ctrl    = imsg->Qualifier &  IEQUALIFIER_CONTROL;
  290.         alt    = imsg->Qualifier & (IEQUALIFIER_LALT | IEQUALIFIER_RALT);
  291.  
  292.         if(shift)
  293.             qual = XMKQ_SHIFT;
  294.         else
  295.         {
  296.             if(alt)
  297.                 qual = XMKQ_ALTERNATE;
  298.             else
  299.             {
  300.                 if(ctrl)
  301.                     qual = XMKQ_CONTROL;
  302.                 else
  303.                     qual = XMKQ_NONE;
  304.             }
  305.         }
  306.  
  307.         if(key = GetHead(con->macrokeys))
  308.         {
  309.             do
  310.             {
  311.                 if(key->xmk_Qualifier == qual)
  312.                 {
  313.                     BOOL match=FALSE;
  314.  
  315.                     if(key->xmk_Type == XMKT_RAWKEY)
  316.                     {
  317.                         if(key->xmk_Code == imsg->Code)
  318.                             match = TRUE;
  319.                     }
  320.  
  321.                     if(key->xmk_Type == XMKT_COOKED)
  322.                     {
  323.                         if(key->xmk_Code == chr)
  324.                             match = TRUE;
  325.                     }
  326.  
  327.                     if(match != FALSE)
  328.                     {
  329.                         con->io->xem_process_macrokeys(key);
  330.                         return(TRUE);
  331.                     }
  332.                 }
  333.             }
  334.             while(key = GetSucc(key));
  335.         }
  336.     }
  337.     return(FALSE);
  338. }
  339.  
  340.  
  341. ULONG __saveds __asm XEmulatorUserMon(register __a0 struct XEM_IO *io, register __a1 UBYTE *retstr, register __d0 ULONG maxlen, register __a2 struct IntuiMessage *imsg)
  342. {
  343.     struct HEXConsole *con = (struct HEXConsole *)io->xem_console;
  344.     struct InputEvent ie;
  345.     ULONG length=0;
  346.     UBYTE *p;
  347.  
  348.  
  349.     if(imsg->Code & IECODE_UP_PREFIX)
  350.         return(0);
  351.  
  352.     p = retstr;
  353.     *p = '\0';
  354.  
  355.    ie.ie_Class            = IECLASS_RAWKEY;
  356.    ie.ie_SubClass        = 0;
  357.     ie.ie_Code            = imsg->Code;
  358.     ie.ie_Qualifier    = imsg->Qualifier;
  359.     ie.ie_position.ie_addr = *((APTR *)imsg->IAddress);
  360.  
  361.     if((length = MapRawKey(&ie, retstr, maxlen, NULL)) <= 0)
  362.         return(0);
  363.  
  364.     *(p+length) = '\0';            /* Null terminate the value */
  365.  
  366.     if(HandleMacroKeys(con, imsg, p[0]))
  367.         length = 0;
  368.  
  369.     return(length);
  370. }
  371.  
  372.  
  373. ULONG __saveds __asm XEmulatorHostMon(register __a0 struct XEM_IO *io, register __a1 struct XEmulatorHostData *hd, register __d0 ULONG actual)
  374. {
  375.     if(hd->Destination != NULL)
  376.     {
  377.         ULONG cnt;
  378.         UBYTE *read, *write;
  379.         REGISTER UBYTE c;
  380.  
  381.         read    = hd->Source;
  382.         write = hd->Destination;
  383.         for(cnt=0; cnt < actual; cnt++)
  384.         {
  385.             c = *read++;
  386.  
  387.             if(c == '\x18'  ||  c == '\x1A')    /* CAN  ||  SUB */
  388.             {
  389.                 hd->InESC = FALSE;
  390.                 hd->InCSI = FALSE;
  391.                 continue;
  392.             }
  393.  
  394.             if(hd->InESC)        /* Escape - Befehl ? */
  395.             {
  396.                 if(c == '[')    /* ist's ein verkappter CSI ? */
  397.                 {
  398.                     hd->InCSI = TRUE;
  399.                     hd->InESC = FALSE;
  400.                 }
  401.                 else
  402.                 {
  403.                     if(c >= '0')
  404.                         hd->InESC = FALSE;
  405.                 }
  406.                 continue;
  407.             }
  408.  
  409.             if(hd->InCSI)        /* CSI - Befehl ? */
  410.             {
  411.                 if(c >= '@')
  412.                     hd->InCSI = FALSE;
  413.                 continue;
  414.             }
  415.  
  416.  
  417.             if(c == '\033')
  418.             {
  419.                 hd->InESC = TRUE;
  420.                 hd->InCSI = FALSE;
  421.                 continue;
  422.             }
  423.  
  424.             if(c == '\233')
  425.             {
  426.                 hd->InESC = FALSE;
  427.                 hd->InCSI = TRUE;
  428.                 continue;
  429.             }
  430.  
  431.             *write++ = c;
  432.         }
  433.  
  434.         return((ULONG)(write - hd->Destination));
  435.     }
  436.     else
  437.         return(0);
  438. }
  439.  
  440.  
  441. BOOL __saveds __asm XEmulatorClearConsole(register __a0 struct XEM_IO *io)
  442. {
  443.     struct HEXConsole *con = (struct HEXConsole *)io->xem_console;
  444.  
  445.     ProcessData(con, "\014", 1);
  446.     return(TRUE);
  447. }
  448.  
  449.  
  450. BOOL __saveds __asm XEmulatorResetConsole(register __a0 struct XEM_IO *io)
  451. {
  452.     struct HEXConsole *con = (struct HEXConsole *)io->xem_console;
  453.  
  454.     ProcessData(con, "\033c", 2);
  455.     return(TRUE);
  456. }
  457.  
  458.  
  459. BOOL __saveds __asm XEmulatorResetTextStyles(register __a0 struct XEM_IO *io)
  460. {
  461.     struct HEXConsole *con = (struct HEXConsole *)io->xem_console;
  462.  
  463.     ProcessData(con, "\033[m", -1);
  464.     return(TRUE);
  465. }
  466.  
  467.  
  468. BOOL __saveds __asm XEmulatorResetCharset(register __a0 struct XEM_IO *io)
  469. {
  470.     struct HEXConsole *con = (struct HEXConsole *)io->xem_console;
  471.  
  472.     ProcessData(con, "\017", 1);
  473.     return(TRUE);
  474. }
  475.  
  476.  
  477. BOOL __saveds __asm XEmulatorOptions(register __a0 struct XEM_IO *io)
  478. {
  479.     return(TRUE);
  480. }
  481.  
  482.  
  483. ULONG __saveds __asm XEmulatorGetFreeMacroKeys(register __a0 struct XEM_IO *io)
  484. {
  485.     return(512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1);
  486. }
  487.  
  488.  
  489. BOOL __saveds __asm XEmulatorMacroKeyFilter(register __a0 struct XEM_IO *io, register __a1 struct XEmulatorMacroKey *macrokeys)
  490. {
  491.     struct HEXConsole *con = (struct HEXConsole *)io->xem_console;
  492.  
  493.     if(con != NULL  &&  con->io->xem_process_macrokeys)
  494.     {
  495.         con->macrokeys = macrokeys;
  496.         return(TRUE);
  497.     }
  498.  
  499.     return(FALSE);
  500. }
  501.  
  502.  
  503. #include <devices/conunit.h>
  504.  
  505. LONG __saveds __asm XEmulatorInfo(register __a0 struct XEM_IO *io, register __d0 ULONG type)
  506. {
  507.     struct HEXConsole *con = (struct HEXConsole *)io->xem_console;
  508.     LONG result = -1;
  509.  
  510.     if(con != NULL)
  511.     {
  512.         struct ConUnit    *unit = (struct ConUnit *)con->wcon->io_Unit;
  513.  
  514.         switch(type)
  515.         {
  516.             case XEMI_CURSOR_POSITION:
  517.                 result = XEMI_CREATE_POSITION(unit->cu_YCCP+1, unit->cu_XCCP+1);
  518.             break;
  519.  
  520.             case XEMI_CONSOLE_DIMENSIONS:
  521.                 result = XEMI_CREATE_DIMENSIONS(unit->cu_XMax+1, unit->cu_YMax+1);
  522.             break;
  523.  
  524.         }
  525.     }
  526.  
  527.     return(result);
  528. }
  529.  
  530.  
  531. BOOL __saveds __asm XEmulatorPreferences(register __a0 struct XEM_IO *io, register __a1 STRPTR filename, register __d0 ULONG mode)
  532. {
  533.     return(TRUE);
  534. }
  535.  
  536.  
  537. STATIC VOID HEX_internalsettings(struct HEXConsole *con)
  538. {
  539. }
  540.  
  541.  
  542. STATIC BOOL HEX_parseoptions(struct HEXConsole *con, STRPTR optionsBuffer)
  543. {
  544.     return(TRUE);
  545. }
  546.  
  547.  
  548. VOID __saveds __asm XEmulatorNewSize(register __a0 struct XEM_IO *io, register __a1 struct IBox *frame)
  549. {
  550. }
  551.  
  552.  
  553. /* end of source-code */
  554.